package com.zsk.blog.controller;

import com.zsk.blog.entity.User;
import com.zsk.blog.repository.UserRepository;
import com.zsk.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserInfoHandler {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @GetMapping("/userinfo")
    public Map<String,User> getUserInfo(){
        Map<String,User> data = new HashMap<String, User>();
        List<User> list =  userRepository.findAll();
        User userinfo = list.get(0);
        data.put("data",userinfo);
        return data;
    }

    @PostMapping("/userdetails")
    public Map<String, Object> getUserDetaisl(@RequestBody Map<String,String> data){
        System.out.println(String.valueOf(data.get("account")));
        System.out.println(String.valueOf(data.get("passwrod")));
        Map response = new HashMap<String,String>();
        if (String.valueOf(data.get("account")).equals("null")){
            response.put("code","400");
            response.put("message","无效用户名");
        }else if (String.valueOf(data.get("password")).equals("null")){
            response.put("code","400");
            response.put("message","无效密码");
        }else {
            User user = userService.findByAccount(data.get("account"));
            if (user != null) {
                if (String.valueOf(data.get("password")).equals(user.getPassword())){
                    response.put("code","200");
                    response.put("data",user);
                }else  {
                    response.put("code","400");
                    response.put("message","密码错误");
                }
            }
        }
        return response;
    }
}
